package com.hlhj.carview.customView

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.View
import android.widget.TextView
import com.hlhj.carview.Constant
import com.hlhj.carview.R
import com.hlhj.carview.aty.WebAty
import kotlinx.android.synthetic.main.dilalog_privacy.*

/**
 *NeverMore
 *2020/9/10
 **/
class DilaogPrivacy(private val c:Context):Dialog(c, R.style.MyDialog) {
    val tvStr="您可阅读《服务协议》和《隐私政策》了解详细信息。如您同意，请点击\"同意\"按钮开始接受我们的服务。"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dilalog_privacy)


        val ssb = SpannableStringBuilder()
        ssb.append(tvStr)
        val start = tvStr.indexOf("《")//第一个出现的位置
        ssb.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                val intent = Intent(context, WebAty::class.java)
                intent.putExtra("html", Constant.BuyContract)
                intent.putExtra("name","服务协议")

                context.startActivity(intent)
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = Color.parseColor("#FE475C")      //设置文件颜色
                // 去掉下划线
                ds.isUnderlineText = false
            }

        }, start, start + 6, 0)

        val end = tvStr.lastIndexOf("《")//最后一个出现的位置
        ssb.setSpan(object : ClickableSpan() {

            override fun onClick(widget: View) {
                val intent = Intent(context, WebAty::class.java)
                intent.putExtra("url",Constant.privacy)
                intent.putExtra("name","隐私政策")

                context.startActivity(intent)
            }
            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.color = context.getResources().getColor(R.color.thme_color)       //设置文件颜色
                // 去掉下划线
                ds.isUnderlineText = false
            }

        }, end, end + 6, 0)

        tvText.setMovementMethod(LinkMovementMethod.getInstance())
        tvText.setText(ssb, TextView.BufferType.SPANNABLE)

        btNo.setOnClickListener {
            dismiss()
        }
        btYes.setOnClickListener { dismiss() }


    }
}